*Input file summary
use "$data\Master file11.dta", clear
append using "$data\Master file12.dta"
append using "$data\Master file13.dta"
append using "$data\Master file14.dta"
gen admits = 1
bysort prod aha_hnpi yr: gen hospitals = _n == 1

collapse (sum) admits hospitals, by(yr prod hosp_state) fast
export excel using "$exp\Summary stats_`d'.xls", sheet("IP admits") sheetreplace cell(A1) firstrow(variables)

*check that only analysis areas used in analysis are in summary stats
use "$temp\cbsa_pos_all years.dta", clear
append using "$temp\cbsa_ppo_all years.dta"
merge m:1 prod analysis_area using "$temp\areas.dta"
keep if _m == 3
bysort analysis_area prod: keep if _N>10
gen sys = aha_hnpi != sys_hosp

*Descriptive Stats 
preserve
	*keep only one obs per hospital
	bysort yr prod aha_hnpi: keep if _n == 1
	gen hosps = 1
	collapse (sum) hosps (mean) hosp_price sys bdtot pct_mcr pct_mcd teaching forprofit ftres non_cbsa ///
	(sd) sd_hosp_price=hosp_price sd_sys=sys sd_bdtot=bdtot sd_pct_mcr=pct_mcr sd_pct_mcd=pct_mcd sd_teaching=teaching sd_forprofit=forprofit sd_ftres=ftres sd_non_cbsa=non_cbsa, by(yr prod) fast	
	
	export excel using "$output\Summary stats_`d'.xls", sheet("Hosp Stats yr") sheetreplace cell(A1) firstrow(variables)
restore	
 
preserve
	gen hosps = 1
	collapse (sum) hosps (mean) hosp_price sys bdtot pct_mcr pct_mcd teaching forprofit ftres non_cbsa ///
	(sd) sd_hosp_price=hosp_price sd_sys=sys sd_bdtot=bdtot sd_pct_mcr=pct_mcr sd_pct_mcd=pct_mcd sd_teaching=teaching sd_forprofit=forprofit sd_ftres=ftres sd_non_cbsa=non_cbsa, by(prod) fast	
	
	export excel using "$output\Summary stats_`d'.xls", sheet("Hosp Stats prod") sheetreplace cell(A1) firstrow(variables)
restore	

preserve
	gen hosps = 1
	collapse (sum) hosps (mean) hosp_price sys bdtot pct_mcr pct_mcd teaching forprofit ftres non_cbsa ///
	(sd) sd_hosp_price=hosp_price sd_sys=sys sd_bdtot=bdtot sd_pct_mcr=pct_mcr sd_pct_mcd=pct_mcd sd_teaching=teaching sd_forprofit=forprofit sd_ftres=ftres sd_non_cbsa=non_cbsa, fast	
	
	export excel using "$output\Summary stats_`d'.xls", sheet("Hosp Stats all") sheetreplace cell(A1) firstrow(variables)
restore	

*Distribution of mean analysis area characteristics
bysort yr analysis_area: gen areas = _n == 1
bysort yr analysis_area aha_hnpi: gen hosp_flag = _n == 1
bysort yr analysis_area: egen hosp_count = total(hosp_flag)
	
bysort analysis_area yr: egen tot_ipdays = total(ipdtot)
gen bed_days = bdtot*365
bysort analysis_area yr: egen tot_beddays = total(bed_days)
gen hosp_util = tot_ipdays/tot_beddays 
gen out_shr = area_out_pts/area_pts

*Overall 
sum hosp_price, det
sum wtp_pp, det
sum cbsa_ins_hhi, det

preserve
	collapse (mean) hosp_count patients hosp_price wtp_pp sys bdtot pct_mcr pct_mcd teaching forprofit ///
	ftres non_cbsa cbsa_ins_hhi pct_lt200pl pct_unins hosp_util out_shr, fast
export excel using "$output\Area summary stats_`d'.xls", sheet("Area Avg") sheetreplace cell(A1) firstrow(variables)
restore	

preserve
	collapse (sd) hosp_count patients hosp_price wtp_pp sys bdtot pct_mcr pct_mcd teaching forprofit ///
	ftres non_cbsa cbsa_ins_hhi pct_lt200pl pct_unins hosp_util out_shr, fast
export excel using "$output\Area summary stats_`d'.xls", sheet("Area SD") sheetreplace cell(A1) firstrow(variables)
restore	

preserve
	collapse (median) hosp_count patients hosp_price wtp_pp sys bdtot pct_mcr pct_mcd teaching forprofit ///
	ftres non_cbsa cbsa_ins_hhi pct_lt200pl pct_unins hosp_util out_shr, fast
export excel using "$output\Area summary stats_`d'.xls", sheet("Area Med") sheetreplace cell(A1) firstrow(variables)
restore	

preserve
	collapse (mean) hosp_count patients hosp_price wtp_pp sys bdtot pct_mcr pct_mcd teaching forprofit ///
	ftres non_cbsa cbsa_ins_hhi pct_lt200pl pct_unins hosp_util out_shr, by(prod) fast
export excel using "$output\Area summary stats_`d'.xls", sheet("Area Avg Prd") sheetreplace cell(A1) firstrow(variables)
restore	

preserve
	collapse (sd) hosp_count patients hosp_price wtp_pp sys bdtot pct_mcr pct_mcd teaching forprofit ///
	ftres non_cbsa cbsa_ins_hhi pct_lt200pl pct_unins hosp_util out_shr, by(prod) fast
export excel using "$output\Area summary stats_`d'.xls", sheet("Area SD Prd") sheetreplace cell(A1) firstrow(variables)
restore	

preserve
	collapse (median) hosp_count patients hosp_price wtp_pp sys bdtot pct_mcr pct_mcd teaching forprofit ///
	ftres non_cbsa cbsa_ins_hhi pct_lt200pl pct_unins hosp_util out_shr, by(prod) fast
export excel using "$output\Area summary stats_`d'.xls", sheet("Area Med Prd") sheetreplace cell(A1) firstrow(variables)
restore	

sum hosp_price if prod == "POS", det
sum wtp_pp if prod == "POS", det
sum cbsa_ins_hhi if prod == "POS", det

sum hosp_price if prod == "PPO", det
sum wtp_pp if prod == "PPO", det
sum cbsa_ins_hhi if prod == "PPO", det

foreach yr in 11 12 13 14 {
foreach prd in POS PPO {
	use "$temp\summary_`prd'_`yr'_data", clear
	bysort (prod) egen total_pts = total(patients)  
	gen cells = 1
	collapse (sum) cells patients (mean) group_size check=patients (sd) sd_group_size=group_size sd_check=patients, by(prod group_chars total_pts) fast
	sort prod group_chars
	export excel using "$output\WTP sum stats_`d'.xls", sheet("Count_`prd'_`yr'") sheetreplace cell(A1) firstrow(variables)
	}
}

exit
